runBlocking()
is another coroutine builder. As the name suggests, it is a
blocking call. It takes a code block and executes it, only continuing once
that code block completes.
This may sound silly.
However, runBlocking()
has one key feature that makes it important: it
executes the code block as a coroutine, so that code block can call suspend
functions. Hence, runBlocking()
is used in cases where you need to use
suspend
functions from code that otherwise is not in position to use
coroutines, such as some types of tests.
Note that runBlocking()
only works on Kotlin/JVM and Kotlin/Native. If
you attempt to run this code snippet here in the Klassbook, it will fail
with a compile error.
You can learn more about this in:
Tags: